package com.ssyc.student.activity;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.OnloadingMoreListener;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StNotifyAdapter;
import com.ssyc.student.bean.NotifyInfo;
import com.ssyc.student.bean.ResultInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 通知消息界面
 * Created by Kevin on 2018/4/20.
 */

public class StudentNotifyActivity extends BaseActivity
        implements View.OnClickListener, SrfManager.OnPullRefreshListener, OnloadingMoreListener {


    //返回按钮
    private ImageView iv_back;

    //删除通知的按钮
    private ImageView iv_delete;


    //加载布局
    private RelativeLayout rlLoading;

    //下拉刷新控件
    private SmartRefreshLayout srf;

    //列表控件
    private RecyclerView rv;

    //列表的数据集合
    private List<NotifyInfo.DataBean> oldDatas;

    //空视图
    private View emptyView;

    //适配器
    private StNotifyAdapter adapter;

    //分页
    private int page = 0;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_notify;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initview();

        //设置下拉控件
        initSrl();

        //设置RecyclerView
        initRv();

        //获取数据
        http(BqaManager.NORML);
    }

    /**
     * 请求服务器数据的方法
     */
    private void http(int method) {
        if (method == BqaManager.NORML || method == BqaManager.PULLREFRESH) {
            page = 0;
        } else {
            page++;
        }

        Map<String, String> map = new HashMap<>();
        map.put("type", "2");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", "0");
        map.put("page", page + "");
        map.put("platform", "8");

        HttpUtils.post(HttpAdress.HENGMSG, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoading) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast("网络错误", false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (null != rlLoading) {
                    rlLoading.setVisibility(View.GONE);
                }
                NotifyInfo info = null;
                try {
                    info = GsonUtil.jsonToBean(response, NotifyInfo.class);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("test", "数据解析异常");
                }
                if (null != info) {
                    if ("200".equals(info.getState())) {
                        //表示返回成功
                        List<NotifyInfo.DataBean> newDatas = info.getData();
                        if (method == BqaManager.NORML) {
                            oldDatas.addAll(newDatas);
                            BqaManager.setRv(emptyView, StudentNotifyActivity.this, adapter, rv, StudentNotifyActivity.this);
                        } else {
                            //更新数据
                            BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                        }
                    } else {
                        Log.i("test", info.getState());
                    }

                }
            }
        });
    }

    /**
     * 初始化视图的方法
     */
    private void initview() {
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        srf = findViewById(R.id.srf);
        rv = findViewById(R.id.rv);
        rlLoading = findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
        iv_delete = findViewById(R.id.iv_delete);
        iv_delete.setOnClickListener(this);
    }

    /**
     * 设置下拉刷新控件
     */
    private void initSrl() {
        SrfManager.setSmartByPullListener(srf, this);
    }


    /**
     * 设置RecyclerView
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        adapter = new StNotifyAdapter(this, R.layout.student_rv_notify, oldDatas);
    }


    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.iv_delete) {
            //删除通知的操作
            doDeleteNoticeAction();
        }

    }

    /**
     * 删除的通知的操作
     */
    private void doDeleteNoticeAction() {
        if (null == oldDatas || oldDatas.size() == 0) {
            //没有通知了
            UiUtils.Toast("没有通知消息需要删除", false);
            return;
        } else {
            //需要删除通知
            PopUpManager.showPop(StudentNotifyActivity.this, R.layout.st_pop_clean_cach, 0.3f, StudentNotifyActivity.this, new PopUpManager.onGetViewListener() {
                @Override
                public void getChildView(View view, int layoutResId) {
                    //关闭按钮
                    ImageView ivClose = view.findViewById(R.id.lv_close);
                    //提示信息
                    TextView tvMessage = view.findViewById(R.id.tv_message);
                    tvMessage.setText("确定删除所有通知吗?");
                    Button btCommit = view.findViewById(R.id.bt_commit);
                    ivClose.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //关闭弹框
                            PopUpManager.dismiss();
                        }
                    });
                    btCommit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //清除通知
                            cleanAction();
                        }
                    });
                }
            });
        }

    }

    /**
     * 清除通知的请求服务器的操作
     */
    private void cleanAction() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "3");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "8");
        HttpUtils.post(HttpAdress.DELETENOTIFY, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                PopUpManager.dismiss();
                UiUtils.Toast("删除失败,网络错误", false);
            }

            @Override
            public void onResponse(String response, int id) {
                PopUpManager.dismiss();
                ResultInfo info = null;
                if (!TextUtils.isEmpty(response)) {
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.i("test", "数据解析异常");
                    }
                    if (info != null) {
                        if ("200".equals(info.state)) {
                            //改变页面数据源
                            if (oldDatas != null) {
                                oldDatas.clear();
                                adapter.notifyDataSetChanged();
                            }
                            //删除成功
                            UiUtils.Toast("删除成功", false);
                        } else {
                            UiUtils.Toast("删除失败,错误码是:" + info.state, false);
                            Log.i("test", "错误码是:" + info.state);
                        }
                    }
                }
            }
        });
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        http(BqaManager.LOADINDGMORE);
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }
}
